package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsCouponHistoryMapper;
import com.itheima.pojo.SmsCouponHistory;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponHistoryImpl implements SmsCouponHistoryApi {
    @Autowired
    private SmsCouponHistoryMapper mapper;

    @Override
    public PageResultVo getPages(Integer couponId, Integer pageNum,
                                 Integer pageSize, Integer useStatus, String orderSn) {

        Page<SmsCouponHistory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCouponHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(couponId !=null, SmsCouponHistory::getCouponId, couponId);
        wrapper.eq(useStatus !=null, SmsCouponHistory::getUseStatus, useStatus);
        wrapper.eq(orderSn!=null,SmsCouponHistory::getOrderSn, orderSn);
        Page<SmsCouponHistory> historyPage = mapper.selectPage(page, wrapper);
        if (ObjectUtil.isEmpty(historyPage)) return new PageResultVo();
        PageResultVo vo = new PageResultVo();
        vo.setTotal((int) historyPage.getTotal());
        vo.setPageNum(pageNum);
        vo.setPageSize(pageSize);
        vo.setList(historyPage.getRecords());
        vo.setTotalPages((int) historyPage.getPages());
        return vo;
    }
}
